﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Sector7.Restaurant.Modules.BIZZ.Utility;
using Sector7.Restaurant.Modules.BIZZ.LocalService;
using System.Collections;
using Sector7.Restaurant.Biz;
using Sector7.Framework.Utility;
using Sector7.Framework.Enums;
using Sector7.Framework.Core;

namespace Sector7.Restaurant.Modules.BIZZ.Windows
{
    /// <summary>
    /// OrderDishesWindow.xaml 的交互逻辑
    /// </summary>
    public partial class SalesWindow : Window
    {
        private RS_SalesBill _salesBill = new RS_SalesBill();

        public RS_SalesBill SalesBill
        {
            get { return _salesBill; }
        }

        public SalesWindow(RS_Table table)
        {
            InitializeComponent();

            this.Owner = Senvironments.MainWindow;

            if (_salesBill.Items == null)
                _salesBill.Items = new List<RS_SalesBillItem>();

            _salesBill.Table = table;
            _salesBill.OccurTime = DateTime.Now;
            _salesBill.TableId = table.TableId;
            _salesBill.Status = (int)BillStatusType.Inprogress;
            _salesBill.Remarks = string.Empty;
            _salesBill.OperatorId = (long)ReflectionUtil.GetPropertyValue(Environments.CurrentAccount, "UniqueId");
            _salesBill.CheckerId = (long)ReflectionUtil.GetPropertyValue(Environments.CurrentAccount, "UniqueId");
        }

        private void LoadData()
        {
            dtgType.ItemsSource = Aide.LocalClient.Invoke<List<RS_MenuItemType>>(
                service => service.GetAllMenuItemTypes(true));
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            LoadData();

            salesDetailControl.SalesBill = _salesBill;
        }

        private void dtgType_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            RS_MenuItemType selectedType = dtgType.SelectedItem as RS_MenuItemType;
            if (selectedType != null)
                ClientUtil.LoadItemsSource(dtgMenuItem, selectedType.MenuItems);
        }

        private void dtgMenuItem_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            RS_MenuItem menuItem = dtgMenuItem.SelectedItem as RS_MenuItem;
            if (menuItem != null)
            {
                RS_SalesBillItem billItem = null;

                billItem = _salesBill.Items.Find(delegate(RS_SalesBillItem salesBillItem)
                {
                    if (salesBillItem.MenuItemId == menuItem.MenuItemId)
                        return true;
                    else
                        return false;
                });

                if (billItem == null)
                {
                    billItem = new RS_SalesBillItem();
                    billItem.MenuItemId = menuItem.MenuItemId;
                    billItem.UnitPrice = menuItem.UnitPrice;
                    billItem.MenuItem = menuItem;
                    billItem.Unit = menuItem.Unit;
                    billItem.UnitId = menuItem.Unit.UnitId;

                    _salesBill.Items.Add(billItem);
                }

                ++billItem.Number;

                _salesBill.Amount += menuItem.UnitPrice;
            }
        }

        private void DataGrid_Loaded(object sender, RoutedEventArgs e)
        {
            DataGrid dataGrid = sender as DataGrid;
            if (dataGrid.HasItems)
            {
                dataGrid.SelectedIndex = 0;
            }
        }

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            string result = Aide.LocalClient.Invoke<string>(
                service => service.AddSalesBill(_salesBill));

            _salesBill.BillId = result;

            if (result != null)
                this.DialogResult = true;
            else
                this.DialogResult = false;
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    }
}
